#!/usr/bin/perl

use strict;
use warnings;
use Algorithm::NaiveBayes;
use Data::Dumper;
use File::Tail;
use Lingua::StopWords qw/getStopWords/;
use Math::Round qw/round/;
use IO::File;

$|++;


my $appli_name = shift;
my $trainFile = shift;
my $logFile = shift;
my $outputFile = shift;
my $uncertainFile = shift;

my $fh = new IO::File "$outputFile", "a+";
autoflush $fh 1;
my $ufh=undef;

if (defined ($uncertainFile)) {
	$ufh = new IO::File "$outputFile", "a+";
	autoflush $ufh 1;
}
# Handle Log
sub handleResult {
	my $header=shift;
	my $result=shift;
	my @predict=();

	my $i=1;

	for my $label ( sort { $$result{$b} <=> $$result{$a} } keys %$result) {
		last if $i > 3;
		push @predict, $label, round (100*$$result{$label});
		
		$i++;
	}

	my @headers=split /;/, $header;

	print $fh $headers[2], ";", $headers[1], ";", $headers[3], ";", join (";", @predict), "\n";
}

# Naives Bayes Object
my $nb=Algorithm::NaiveBayes->new;

#Misc Var
my $line="";
my $count={};
my @corpus_list=();
my $total_lines=0;
my $stopWords=undef;
my $result;
my $unit="";
my $headerLine="";
# Loading Stop Word list
$stopWords=getStopWords('en');
for my $elt( keys %{getStopWords('fr')}) { $$stopWords{$elt}++;}

#Loading previous state
if ( -f "$trainFile") {
    $nb = $nb->restore_state($trainFile);
} else {
    print STDERR "> Corpus doesnt exists, exit from analyser...\n";
    exit 1;
}

my $file=File::Tail->new( name=> $logFile, maxinterval => 5, resetAfter=> 200);
while (defined($line=$file->read)) {
    if ( $line =~ /;$appli_name;/ ) {
    	$headerLine=$line;	
	if ( $unit !~ /^\s*$/ ) {
	    $unit =~ s/\n/ /g;
	    $unit =~ s/;/ /g;
	    $unit =~ s/\)/ /g;
	    $unit =~ s/\(/ /g;
	    $unit =~ s/:/ /g;
	    $unit =~ s/=/ /g;
	    $unit =~ s/,/ /g;
	    $unit =~ s/\./ /g;
	    $unit =~ s/\n/ /g;
	    $unit =~ s/</ /g;
	    $unit =~ s/>/ /g;
	    $unit =~ s/\// /g;
	    $unit =~ s/\s+/ /g;
	    my @tokens=split / /, $unit;

	    foreach my $word ( grep { !$stopWords->{$_} } @tokens) {
		$$count{$word}++;
	    }
	    
	    $result=$nb-> predict ( attributes => $count  );
 	    
	    handleResult ("$headerLine", $result);
	    $count={};
	}
	$unit=$line;
    } else {
	$unit.=$line;
    }

}
close ($fh);
close ($ufh) if defined $uncertainFile;
exit(0);
